package DigiJoe;

import java.awt.Image;
import java.util.ArrayList;

public class ComponentImage {

    //image for the on off state
    private Image onImage, offImage;
    
    //coordinate holding the position inside the canvas
    private Coordinate position;
    
    //id (same as in model), inportnumber, outportnumber
    private int id, inPort, outPort;
    
    //state
    private boolean onOff;
    
    //port offsets
    private ArrayList<Coordinate> inPortList, outPortList;

    public ComponentImage(Image OnImage, Image OffImage, Coordinate Position, int Id, int InPort, int OutPort) {
        //init
        onImage = OnImage;
        offImage = OffImage;
        position = Position;
        id = Id;
        inPort = InPort;
        outPort = OutPort;
        onOff = true;
        inPortList = new ArrayList<Coordinate>();
        outPortList = new ArrayList<Coordinate>();

        //we need to store the inport and outport pixel offsets so it aligns with the image

        //input
        switch (inPort) {
            case 1:
                inPortList.add(new Coordinate(0, 45));
                break;
            case 2:
                inPortList.add(new Coordinate(0, 33));
                inPortList.add(new Coordinate(0, 58));
                break;
            default:
                //this can only be composit offset is at +12px so lets divide the ports equaly from +12 to 86 px
                if (InPort > 2) {
                    int incCount = (86 - 12) / inPort;
                    for (int i = 0; i < inPort; i++) {
                        inPortList.add(new Coordinate(0, 14 + i * incCount));

                    }
                }
                break;
        }

        //output
        switch (outPort) {
            case 1:
                outPortList.add(new Coordinate(100, 45));
                break;
            default:
                //this can only be composit
                if (outPort > 1) {
                    int incCount = (86 - 12) / outPort;
                    for (int i = 0; i < inPort; i++) {
                        outPortList.add(new Coordinate(100, 14 + i * incCount));

                    }
                }
                break;
        }
    }

    public int getId() {
        return id;
    }

    //Kép visszaadása
    public Image getImage() {
        if (onOff) {
            return onImage;
        } else {
            return offImage;
        }
    }

    //Visszadja a megfelelő bemeneti port koordinátáit
    public Coordinate getInPort(int port) throws Exception {
        if (port > inPortList.size() - 1) {
            throw new Exception();
        }
        return position.add(inPortList.get(port));
    }

    //Visszadja a megfelelő kimeneti port koordinátáit
    public Coordinate getOutPort(int port) throws Exception {
        if (port > outPortList.size() - 1) {
            throw new Exception();
        }
        return position.add(outPortList.get(port));
    }

    public Coordinate getPosition() {
        return position;
    }

    //Igaz, ha a kattintás a komponens épén belül van
    public boolean isInside(int X, int Y) {
        if (X >= position.getX() && X <= position.getX() + onImage.getWidth(null)
                && Y >= position.getY() && Y <= position.getY() + onImage.getHeight(null)) {
            return true;
        }
        return false;
    }

    //setter
    public void setOnOff(boolean isOn) {
        onOff = isOn;
    }
}
